#![allow(unused_variables)]

#[derive(Debug)]
struct File {
    name: String,
    data: Vec<u8>,
}

fn open(f: &mut File) -> bool {
    true
}
fn close(f: &mut File) -> bool {
    true
}

fn read(f: &File, save_to: &mut Vec<u8>) -> usize {
    //不返回数据
    let mut tmp = f.data.clone();
    let read_length = tmp.len();

    save_to.reserve(read_length); //分配内存
    save_to.append(&mut tmp);
    read_length
}

fn main() {
    let mut f2 = File {
        name: String::from("2.txt"),
        data: vec![114, 117, 115, 116, 33],
    };

    let mut buffer: Vec<u8> = vec![];

    //与文件交互
    open(&mut f2);
    let f2_length = read(&f2, &mut buffer);
    close(&mut f2);

    let text = String::from_utf8_lossy(&buffer); // 将Vec<u8>转为String,无效的字符会被换成乱码

    println!("{:?}", f2);
    println!("{} is {} bytes long", &f2.name, f2_length);
}
